package com.lyshop.jinding.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.lyshop.jinding.R;
import com.lyshop.jinding.adapters.MessageListAdapter;
import com.lyshop.jinding.base.BaseActivity;
import com.lyshop.jinding.base.BaseRVAdapter;
import com.lyshop.jinding.bean.UserMessageBean;
import com.lyshop.jinding.net.ActionCallbackListener;
import com.lyshop.jinding.utils.RouteUtil;
import com.lyshop.jinding.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends BaseActivity implements View.OnClickListener, OnRefreshListener, OnLoadMoreListener {

    private SmartRefreshLayout refresh_layout;
    private RecyclerView rv_list;
    private TextView tv_right;
    private TextView tv_no_message;

    private List<UserMessageBean> mMessageList;
    private MessageListAdapter mMessageListAdapter;

    private int mPageIndex = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMessageList = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_list.setLayoutManager(manager);
        mMessageListAdapter = new MessageListAdapter(this, mMessageList);
        rv_list.setAdapter(mMessageListAdapter);
        mMessageListAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                UserMessageBean bean = (UserMessageBean) data.get(position);
                if (bean.send_type == 0){
                    editMessage(bean.msg_id);
                }
                RouteUtil.routePageByUrl(MessageActivity.this, bean.link);
            }
        });
    }


    @Override
    protected int setContentView() {
        return R.layout.activity_message;
    }

    @Override
    protected void findViews() {
        refresh_layout = findViewById(R.id.refresh_layout);
        rv_list = findViewById(R.id.rv_list);
        tv_right = findViewById(R.id.tv_right);
        tv_no_message = findViewById(R.id.tv_no_message);

        refresh_layout.setEnableRefresh(true);

    }

    @Override
    protected void setListeners() {
        tv_right.setOnClickListener(this);
        refresh_layout.setOnRefreshListener(this);
        refresh_layout.setOnLoadMoreListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right:
                allMsgEdit();
                break;
        }
    }

    public void allMsgEdit() {
        StringBuffer buffer = new StringBuffer();
        for (UserMessageBean bean : mMessageList) {
            if (bean.send_type == 0) {
                buffer.append(bean.msg_id);
                buffer.append(",");
            }
        }
        String ids = buffer.toString();
        if (ids.lastIndexOf(",") > 0 && ids.lastIndexOf(",") == ids.length() - 1) {
            ids = ids.substring(0, ids.length() - 1);
        }
        if (ids.length() > 0) {
            //标记已读
            editMessage(ids);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMsgList();
    }

    private void getMsgList() {
        mAppAction.getMsgList(mPageIndex, new ActionCallbackListener<List<UserMessageBean>>() {
            @Override
            public void onSuccess(List<UserMessageBean> data) {
                cancelLoading();
                if (null != data && data.size() > 0) {
                    if (mPageIndex == 1) {
                        mMessageList = data;
                    } else {
                        mMessageList.addAll(data);
                    }
                    mMessageListAdapter.setData(mMessageList);

                    if (data.size() == 10) {
                        refresh_layout.setEnableLoadMore(true);
                    } else {
                        refresh_layout.setEnableLoadMore(false);
                    }
                }

                if (mMessageList.size() > 0) {
                    tv_no_message.setVisibility(View.GONE);
                } else {
                    tv_no_message.setVisibility(View.VISIBLE);
                }
                completeRefresh();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                completeRefresh();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void editMessage(String id) {
        loading();
        mAppAction.editPushMsg(id, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                getMsgList();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void completeRefresh() {
        refresh_layout.finishRefresh(1000);
        refresh_layout.finishLoadMore(1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mPageIndex++;
        getMsgList();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPageIndex = 1;
        getMsgList();
    }
}
